package com.secsbrain.cmdb.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.secsbrain.sso.core.SsoHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.secsbrain.cmdb.biz.INetworkDeviceService;
import com.secsbrain.cmdb.biz.impl.CommonService;
import com.secsbrain.common.ResponseResult;
import com.secsbrain.cmdb.dal.model.NetworkDevice;

/**
 * 网络设备控制层
 * @author zhaoyi
 *
 */
@RestController
@RequestMapping("api/networkDevice")
public class NetworkDeviceController{
	
    @Autowired
    INetworkDeviceService networkDeviceService;
    
    @Autowired
    CommonService commonService;
    
    /**
     * 功能描述: <br>
     * 查询列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseResult<?> queryList(Integer page, Integer limit,NetworkDevice params) {
//    	if(StringUtils.isEmpty(params.getDepartment())){
//    		params.setDepartment(null);
//    	}
    	Page<NetworkDevice> pageBean=new Page<NetworkDevice>(page, limit);
    	Page<NetworkDevice> pageResult=networkDeviceService.selectPage(pageBean, params);
    	
    	Map<Long, String> idcMap=commonService.getIdcMap();
    	Map<Long, String> cabinetMap=commonService.getCabinetMap();
    	for(NetworkDevice networkDevice:pageResult.getRecords()){
    		if(networkDevice.getIdcId()!=null){
    			networkDevice.setRackName(idcMap.get(networkDevice.getIdcId())+"/"
    				+cabinetMap.get(networkDevice.getCabinetId()));
    		}
    	}
    	return new ResponseResult(pageResult);
    }
    
    /**
     * 功能描述: <br>
     * 查询所有列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/allList", method = RequestMethod.GET)
    public ResponseResult<?> queryAllList(NetworkDevice params) {
    	List<NetworkDevice> list=networkDeviceService.selectList(params);
    	return new ResponseResult(list);
    }
    
    /**
     * 功能描述: <br>
     * 新增
     *
     * @param networkDevice
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult<?> insert(@RequestBody NetworkDevice networkDevice) {
    	networkDevice.setCreateTime(new Date());
    	networkDevice.setCreator(SsoHelper.currentUser().getNickname());
    	networkDeviceService.insertSelective(networkDevice);
    	return new ResponseResult();
    }
    
    /**
     * 功能描述: <br>
     * 更新
     *
     * @param networkDevice
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public ResponseResult<?> update(@RequestBody NetworkDevice networkDevice) {
    	networkDevice.setUpdateTime(new Date());
    	networkDevice.setUpdator(SsoHelper.currentUser().getNickname());
    	networkDeviceService.updateSelectiveById(networkDevice);
    	return new ResponseResult();
    }
    
    /**
     * 功能描述: <br>
     * 删除
     *
     * @param networkDevice
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public ResponseResult<?> delete(@RequestBody NetworkDevice networkDevice) {
    	networkDeviceService.deleteById(networkDevice.getId());
    	return new ResponseResult();
    }
    
}
